<?php 
class ArticleController extends AuthController{
	private $model;
	public function __init(){
		parent::__init();
		$this->model=K('Article');
	}
	/**
	 * 文章列表展示
	 */
	public function index(){
		//分页
		$total=M()->join('__article__ a JOIN __category__ c ON a.category_cid=c.cid')->where("is_recycle=0")->count();
		//1.总条数 2.每页显示的条数,3.显示的页码的数量
    		$pageObj = new Page($total,5,3);
		//显示分页
		$page=$pageObj->show();
		$this->assign('page',$page);
		//查询文章和分类关联的数据
		$data = M()->join('__article__ a JOIN __category__ c ON a.category_cid=c.cid')->where("is_recycle=0")->limit($pageObj->limit())->all();
		$this->assign('data',$data);
		
		$this->display();
	}
	
	/**
	 * 文章添加
	 */
	public function addArticle(){
		if(IS_POST){

			if(!$this->model->addData()) $this->error($this->model->error);
			$this->success('添加成功',U('index'));
		}
		//处理所属分类
		$this->assign('cateData',Data::tree(K('Category')->all(), 'cname'));
		//处理选择的标签
		$this->assign('tag',K('Tag')->all());
		$this->display();
	}
	
	/**
	 * 上传缩略图
	 */
	     public function upload(){
	        $upload = new Upload('Upload/Content/' . date('y/m'));
	        $file = $upload->upload();
	        if (empty($file)) {
	        		//相当于 echo json_encode('上传失败');exit;
	            $this->ajax('上传失败');
	        } else {
	        		/** $file内部就是以下这个数组
	        		$file = array(
	        			0 => array(
						'path' => 'Upload/Content/15/8/123981239172.jpg'	,
						'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
					 	'image' => 1
					),
				);**/
	        		//上传成功，把上传好的信息返给js
	            $data = $file[0];
	            $this->ajax($data);
	        }
	    }
		 /**
		  * 缩略图删除
		  */
//		  public function delimg(){
//		  	$path=Q("post.path");
//			unlink($path);
//		  }
		  /**
		   * 回收站
		   */
		   public function recycle(){
                $msg=M()->join('__article__ a JOIN __category__ c ON a.category_cid=c.cid')->where('is_recycle =1 ')->all();
				$this->assign('msg',$msg);
				$this->display();
			}
		 /**
		 * 删除到回收站
		 */
		public function delToRecycle(){
			$aid=Q("get.aid",0,'intval');
			//update(array('is_recycle' =>1)) 框架规定
			$this->model->where("aid={$aid}")->update(array('is_recycle' =>1));
			$this->success('删除成功');	
		}
		 
		 /**
		  * 删除
		  */
		  public function del(){
		  	$aid=Q('get.aid',0,'intval');
			//删除分类
			$this->model->where("aid={$aid}")->delete();
			K('ArticleTag')->where("article_aid={$aid}")->delete();
			K('ArticleData')->where("article_aid={$aid}")->delete();
			$this->success('删除成功');
		  }
		  /**
		   * 还原
		   */
	     public function recovery(){
	     	$aid=Q("get.aid",0,'intval');
			$this->model->where("aid={$aid}")->update(array('is_recycle' =>0));
			$this->success('还原成功');
	     	
	     }
		 /**
		  * 编辑
		  */
		  public function editArticle(){
		  	if(IS_POST){
		  		
		  		if(!$this->model->editData()) $this->error($this->model->error);
				$this->success('编辑成功',U('index'));
		  	}
		  	//获取旧数据
		  	$aid=Q("get.aid",0,'intval');
			$oldData=M()->join('__article__ a JOIN __article_data__ ad ON a.aid=ad.article_aid')->where("aid={$aid}")->find();
            //把文章属性变为数组，为了在页面可用in_array()判断属性是否被选中
            $oldData['attr']=explode(',', $oldData['attr']);
//						p($oldData);die;
            $this->assign('oldData',$oldData);
			//获得文章对应的标签
			$tids=K('ArticleTag')->where("article_aid={$aid}")->getField('tag_tid',true);
//			p($tids);
			$this->assign('tids',$tids);
		  	//处理所属分类
			$this->assign('cateData',Data::tree(K('Category')->all(), 'cname'));
			//处理选择的标签
			$this->assign('tag',K('Tag')->all());
			
			
			
		  	$this->display();
		  }
	 
	 
	 
}


 ?>